//
//  BloodOxygenDetailView.swift
//  DaRings
//
//  Created by 易先生 on 2022/11/2.
//

import UIKit
import MYModule

class BloodOxygenDetailView: View {
    
    override func makeUI() {
        super.makeUI()
        
        self.addSubview(spo2Lbl)
        self.addSubview(unitLbl)
        self.addSubview(dateLbl)
        self.addSubview(progressView)
        self.addSubview(bottomStackView)

        spo2Lbl.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(26)
            make.centerX.equalToSuperview().offset(-18)
        }

        unitLbl.snp.makeConstraints { make in
            make.leading.equalTo(spo2Lbl.snp.trailing)
            make.centerY.equalTo(spo2Lbl)
        }

        dateLbl.snp.makeConstraints { make in
            make.top.equalTo(spo2Lbl.snp.bottom).offset(4)
            make.centerX.equalToSuperview()
        }

        progressView.snp.makeConstraints { make in
            make.top.equalTo(dateLbl.snp.bottom).offset(36)
            make.leading.equalToSuperview().offset(26)
            make.trailing.equalToSuperview().offset(-26)
            make.height.equalTo(20)
        }

        bottomStackView.snp.makeConstraints { make in
            make.top.equalTo(progressView.snp.bottom).offset(8)
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.bottom.equalToSuperview().offset(-32)
        }
    }
    
    public lazy var spo2Lbl: Label = {
        let spo2Lbl = Label()
        spo2Lbl.numberOfLines = 0
        spo2Lbl.text = "--"
        spo2Lbl.font = Fonts.boldSystemFont(ofSize: 40)
        spo2Lbl.textColor = UIColor.assist_222222
        return spo2Lbl
    }()

    public lazy var unitLbl: Label = {
        let unitLbl = Label()
        unitLbl.numberOfLines = 0
        unitLbl.text = "%"
        unitLbl.font = Fonts.boldSystemFont(ofSize: 12)
        unitLbl.textColor = UIColor.assist_222222
        return unitLbl
    }()

    public lazy var dateLbl: Label = {
        let dateLbl = Label()
        dateLbl.font = Fonts.systemFont(ofSize: 10)
        dateLbl.numberOfLines = 0
        dateLbl.text = "20 Oct. 2022, Tue"
        dateLbl.textColor = UIColor.assist_222222
        return dateLbl
    }()

//    public func bind(to cellModel: TodaySpo2CellViewModel) {
//        cellModel.date.bind(to: dateLbl.rx.text).disposed(by: rx.disposeBag)
//        cellModel.quantity.bind(to: spo2Lbl.rx.text).disposed(by: rx.disposeBag)
//        cellModel.unit.bind(to: unitLbl.rx.text).disposed(by: rx.disposeBag)
//        if let quantity = cellModel.quantity.value {
//            guard quantity != "--" else {
//                return
//            }
//            let spo2 = Int(Double(quantity)!)
//            DispatchQueue.main.async {
//                self.progressView.configProgress(width: self.getProgressWidth(spo2: spo2))
//            }
//        }
//    }

    private func getProgressWidth(spo2: Int) -> CGFloat {
        let progressWith = progressView.width

        var width = 0.0

        var precent = 0
        if spo2 >= 80 {
            precent = spo2 - 80
        }
        width = CGFloat(Double(Double(precent) / 20.0)) * progressWith
        return width
    }

    public lazy var progressView: TodayBloodOxygenProgressView = {
        let progressView = TodayBloodOxygenProgressView()
        progressView.configColor(bgColor: UIColor.assist_EEEEEE, progressColor: UIColor.BloodOxygen.theme)
        return progressView
    }()

    public lazy var bottomStackView: StackView = {
        let bottomStackView = StackView()
        bottomStackView.alignment = .center
        bottomStackView.axis = .horizontal
        bottomStackView.distribution = .equalSpacing
        for title in ["80%", "85%", "90%", "95%", "100%"] {
            let numberLabel = Label()
            numberLabel.textColor = UIColor.assist_D0D0D0
            numberLabel.font = Fonts.systemFont(ofSize: 10)
            numberLabel.text = title
            bottomStackView.addArrangedSubview(numberLabel)
        }
        return bottomStackView
    }()

}
